package com.gbq.qimo.servlet;

import com.gbq.qimo.pojo.Admin;
import com.gbq.qimo.pojo.Student;
import com.gbq.qimo.pojo.Teacher;
import com.gbq.qimo.service.AdminService;
import com.gbq.qimo.service.StudentService;
import com.gbq.qimo.service.TeacherService;
import com.gbq.qimo.service.impl.AdminServiceImpl;
import com.gbq.qimo.service.impl.StudentServiceImpl;
import com.gbq.qimo.service.impl.TeacherServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @ClassName LoginServlet
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/5/29 13:41
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String verifycode = request.getParameter("verifycode");
        //校验验证码
        String checkcode_server = (String)request.getSession().getAttribute("CHECKCODE_SERVER");
        request.getSession().removeAttribute("CHECKCODE_SERVER");
        if (!checkcode_server.equals(verifycode)){
            request.setAttribute("login_msg","验证码错误！");
            request.getRequestDispatcher("/index.jsp").forward(request,response);
            return;
        }
        Map<String, String[]> map = request.getParameterMap();
//        for (String key:map.keySet()){
//            System.out.println(key);
//            System.out.println(map.get(key)[0]);
//        }

        if (map.get("loginType") == null){
            request.setAttribute("login_msg","请勾选自己的角色!");
            request.getRequestDispatcher("/index.jsp").forward(request,response);
            return;
        }
        //管理员登录
        if (map.get("loginType")[0].equals("admin")){
            Admin admin = new Admin();
            try{
                BeanUtils.populate(admin,map);
                System.out.println(admin);
            }catch (IllegalAccessException e){
                e.printStackTrace();
            }catch (InvocationTargetException e){
                e.printStackTrace();
            }

            AdminService adminService = new AdminServiceImpl();
             Admin loginAdmin = adminService.findUser(admin);
            if (loginAdmin != null){
                request.getSession().setAttribute("user",loginAdmin);
                //跳转主页面
                response.sendRedirect(request.getContextPath()+"/findStudentByPage");
            }else{
                request.setAttribute("login_msg","用户名或密码错误!");
                request.getRequestDispatcher("/index.jsp").forward(request,response);
//            response.sendRedirect(request.getContextPath()+"/index.jsp");
            }


        }else if (map.get("loginType")[0].equals("teacher")){
            Teacher teacher = new Teacher();
            try{
                BeanUtils.populate(teacher,map);
            }catch (IllegalAccessException e){
                e.printStackTrace();
            }catch (InvocationTargetException e){
                e.printStackTrace();
            }

            TeacherService teacherService = new TeacherServiceImpl();
            Teacher loginTeacher = teacherService.findTeacher(teacher);
            if (loginTeacher != null){
                request.getSession().setAttribute("user",loginTeacher);
                //跳转主页面
                response.sendRedirect(request.getContextPath()+"/findStudentByPage");
            }else{
                request.setAttribute("login_msg","用户名或密码错误!");
                request.getRequestDispatcher("/index.jsp").forward(request,response);
//            response.sendRedirect(request.getContextPath()+"/index.jsp");
            }
        }else if(map.get("loginType")[0].equals("student")){
//            System.out.println(1);
            Student student = new Student();
            try{
                BeanUtils.populate(student,map);
                System.out.println(student);
            }catch (IllegalAccessException e){
                e.printStackTrace();
            }catch (InvocationTargetException e){
                e.printStackTrace();
            }
             StudentService studentService = new StudentServiceImpl();
            Student loginStudent = studentService.findStudent(student);
            if (loginStudent != null){
                request.getSession().setAttribute("user",loginStudent);
                //跳转主页面
                response.sendRedirect(request.getContextPath()+"/studentMain");
            } else{
                request.setAttribute("login_msg","用户名或密码错误!");
                request.getRequestDispatcher("/index.jsp").forward(request,response);
//            response.sendRedirect(request.getContextPath()+"/index.jsp");
            }
        }


    }


}
